//
//  YHMachineServiceChargeMakeCell.m
//  YHApp
//
//  Created by -- on 2021/7/24.
//

#import "HAMachineServiceChargeMakeCell.h"
#import "HAMachineServiceChargeMo.h"
#import "YHMachineServiceChargeItemCell.h"

@interface HAMachineServiceChargeMakeCell ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
/***/
//@property (nonatomic, strong) UILabel *titleLabel;

/***/
@property (nonatomic, strong) UIView *customerView;
/***/
@property (nonatomic, strong) UILabel *customerTitLabel;
/***/
@property (nonatomic, strong) UIView *customerBgView;
/***/
@property (nonatomic, strong) UIView *customerLineView;
/***/
@property (nonatomic, strong) UILabel *customerNameTitLabel;
/***/
@property (nonatomic, strong) UILabel *customerNameConLabel;
/***/
@property (nonatomic, strong) UILabel *customerSNTitLabel;
/***/
@property (nonatomic, strong) UILabel *customerSNConLabel;
/***/
@property (nonatomic, strong) UIView *policyView;
/***/
@property (nonatomic, strong) UIView *policyBgView;
/***/
@property (nonatomic, strong) UILabel *policyTitLabel;
/***/
@property (nonatomic, strong) UILabel *policyConLabel;

@end

@implementation HAMachineServiceChargeMakeCell

-(void)setModel:(HAMachineServiceChargeMo *)model {
    _model = model;

//    if (self.model.customer) {
//        h += 44+54+54;
//    }
//    if (![NSString isEmptyString:self.model.policy]) {
//        h += 10 + 54;
//    }

//    self.titleLabel.text = @"机具服务费代收";
    
    if (_model.customer) {
        self.customerView.hidden = NO;
        self.customerNameConLabel.text = _model.customer.surname;
        self.customerSNConLabel.text = _model.customer.number;
    }else {
        self.customerView.hidden = YES;
    }
    
    if (![NSString isEmptyString:_model.policy]) {
        self.policyView.hidden = NO;
        self.policyConLabel.text = _model.policy;
    }else {
        self.policyView.hidden = YES;
    }

    [self.contentView setNeedsLayout];
    [self.tableView reloadData];
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _model.rates.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 54+28+10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YHMachineServiceChargeItemCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YHMachineServiceChargeItemCellID"];
    if (!cell) {
        cell = [[YHMachineServiceChargeItemCell alloc]initWithStyle:0 reuseIdentifier:@"YHMachineServiceChargeItemCellID"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.isNotEdit = _model.disabled;
    cell.rateModel = _model.rates[indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

}

-(void)layoutSubviews {
    [super layoutSubviews];
    
    self.contentView.backgroundColor = [UIColor khaBGColor];
    if (!_model) {
        return;
    };
    
//    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_equalTo(20);
//        make.height.mas_offset(20);
//        make.top.mas_equalTo(17);
//    }];
    
    CGFloat topH = 0;
        
    [self.customerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(topH);
        make.left.top.right.mas_equalTo(0);
        if (_model.customer) {
            make.height.mas_offset(44+54+54);
        }else {
            make.height.mas_offset(0);
        }
    }];
    
    if (_model.customer) {
        topH += 44+54+54;
    }

    
    [self.customerTitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.height.mas_offset(20);
        make.top.mas_equalTo(12);
    }];
    
    [self.customerBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(44);
        make.height.mas_offset(54+54);
    }];
    
    [self.customerLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(20);
        make.height.mas_offset(1);
        make.top.mas_equalTo(53.5);
    }];
    
    [self.customerNameTitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.height.mas_offset(20);
        make.width.mas_offset(80);
        make.top.mas_equalTo(17);
    }];
    
    [self.customerNameConLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-20);
        make.height.mas_offset(20);
        make.left.mas_equalTo(self.customerNameTitLabel.mas_right);
        make.top.mas_equalTo(17);
    }];
    
    [self.customerSNTitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.height.mas_offset(20);
        make.width.mas_offset(80);
        make.bottom.mas_equalTo(-17);
    }];
    
    [self.customerSNConLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-20);
        make.height.mas_offset(20);
        make.left.mas_equalTo(self.customerSNTitLabel.mas_right);
        make.bottom.mas_equalTo(-17);
    }];

    [self.policyView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(topH);
        if (![NSString isEmptyString:_model.policy]) {
            make.height.mas_offset(10+54);
        }else {
            make.height.mas_offset(0);
        }
    }];
    
    if (![NSString isEmptyString:_model.policy]) {
        topH += 10+54;
    }

    
    [self.policyBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(10);
        make.height.mas_offset(54);
    }];
    
    [self.policyTitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.height.mas_offset(20);
        make.width.mas_offset(80);
        make.bottom.mas_equalTo(-17);
    }];
    
    [self.policyConLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-20);
        make.height.mas_offset(20);
        make.left.mas_equalTo(self.policyTitLabel.mas_right);
        make.bottom.mas_equalTo(-17);
    }];

    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(topH);
        make.bottom.mas_equalTo(0);
    }];
}

//- (UILabel *)titleLabel
//{
//    if (!_titleLabel) {
//        _titleLabel = [UILabel ha_NLabelWithFont:16 textColor:@"#858B9C" text:@"" alignment:0];
//        [self.contentView addSubview:_titleLabel];
//    }
//    return _titleLabel;
//}

- (UITableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero];
        // 去掉滚动条
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.contentView addSubview:_tableView];
    }
    return _tableView;
}

- (UIView *)customerView
{
    if (!_customerView) {
        _customerView = [UIView ha_createView];
        _customerView.backgroundColor = [UIColor khaBGColor];
        [self.contentView addSubview:_customerView];
    }
    return _customerView;
}

- (UILabel *)customerTitLabel
{
    if (!_customerTitLabel) {
        _customerTitLabel = [UILabel ha_NLabelWithFont:17 textColor:@"#000000" text:@"商户信息" alignment:0];
        [self.customerView addSubview:_customerTitLabel];
    }
    return _customerTitLabel;
}

- (UIView *)customerBgView
{
    if (!_customerBgView) {
        _customerBgView = [UIView ha_createView];
        _customerBgView.backgroundColor =[ UIColor whiteColor];
        [self.customerView addSubview:_customerBgView];
    }
    return _customerBgView;
}

- (UIView *)customerLineView
{
    if (!_customerLineView) {
        _customerLineView = [UIView ha_createView];
        _customerLineView.backgroundColor = [UIColor kLineColor];
        [self.customerBgView addSubview:_customerLineView];
    }
    return _customerLineView;
}

- (UILabel *)customerNameTitLabel
{
    if (!_customerNameTitLabel) {
        _customerNameTitLabel = [UILabel ha_NLabelWithFont:17 textColor:@"#111a34" text:@"商户编码" alignment:0];
        [self.customerBgView addSubview:_customerNameTitLabel];
    }
    return _customerNameTitLabel;
}

- (UILabel *)customerNameConLabel
{
    if (!_customerNameConLabel) {
        _customerNameConLabel = [UILabel ha_NLabelWithFont:15 textColor:@"#858b9c" text:@"" alignment:2];
        [self.customerBgView addSubview:_customerNameConLabel];
    }
    return _customerNameConLabel;
}

- (UILabel *)customerSNTitLabel
{
    if (!_customerSNTitLabel) {
        _customerSNTitLabel = [UILabel ha_NLabelWithFont:17 textColor:@"#111a34" text:@"商户名称" alignment:0];
        [self.customerBgView addSubview:_customerSNTitLabel];
    }
    return _customerSNTitLabel;
}

- (UILabel *)customerSNConLabel
{
    if (!_customerSNConLabel) {
        _customerSNConLabel = [UILabel ha_NLabelWithFont:15 textColor:@"#858b9c" text:@"" alignment:2];
        [self.customerBgView addSubview:_customerSNConLabel];
    }
    return _customerSNConLabel;
}

- (UIView *)policyView
{
    if (!_policyView) {
        _policyView = [UIView ha_createView];
        _policyView.backgroundColor = [UIColor khaBGColor];
        [self.contentView addSubview:_policyView];
    }
    return _policyView;
}

- (UIView *)policyBgView
{
    if (!_policyBgView) {
        _policyBgView = [UIView ha_createView];
        _policyBgView.backgroundColor = [UIColor whiteColor];
        [self.policyView addSubview:_policyBgView];
    }
    return _policyBgView;
}

- (UILabel *)policyTitLabel
{
    if (!_policyTitLabel) {
        _policyTitLabel = [UILabel ha_NLabelWithFont:17 textColor:@"#111a34" text:@"政策名称" alignment:0];
        [self.policyBgView addSubview:_policyTitLabel];
    }
    return _policyTitLabel;
}

- (UILabel *)policyConLabel
{
    if (!_policyConLabel) {
        _policyConLabel = [UILabel ha_NLabelWithFont:15 textColor:@"#858b9c" text:@"" alignment:2];
        [self.policyBgView addSubview:_policyConLabel];
    }
    return _policyConLabel;
}


@end
